//
//  LXPrepareGetGoodsViewController.m
//  LvXi_newVersion
//
//  Created by 诸葛孔明 on 16/8/12.
//  Copyright © 2016年 SJT. All rights reserved.
//............................................................................................//
//...........................................个人中心-全部订单...........................................//
//............................................................................................//

#import "LXAllOrderViewController.h"

#import "LXPrepareGetGoodsOrderViewCell.h"

#import "OderSectionHeaderView.h"

#import "OderSectionFooterView.h"

@interface LXAllOrderViewController ()<UITableViewDataSource,UITableViewDelegate,OderSectionFooterViewDelegate>

@property (nonatomic,weak) UITableView *tableView;

@property(nonatomic,strong)NSMutableArray * orderList;
@end

@implementation LXAllOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpTableView];
    self.orderList = [NSMutableArray array];
    [[LXDataManager manager] getOrderListWithPaynent:OrderPaymentAll complete:^(BOOL success, id res) {
        if(success){
            [self.orderList addObjectsFromArray:res];
            [self.tableView reloadData];
        }
    }];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setUpNavi];
    self.tableView.contentInset = (UIEdgeInsets){10,0,0,0};
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)setUpNavi{
    [super setUpNavi];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"全部订单";
}
//......................tableView初始化..............................//
- (void)setUpTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor colorWithRed:238 / 255.0 green:238 / 255.0 blue:238 / 255.0 alpha:1.0];
    //    tableView.contentSize = CGSizeMake(ScreenWidth, ScreenHeight);
    tableView.contentInset = UIEdgeInsetsMake(74, 0, 90, 0);
    tableView.showsVerticalScrollIndicator = NO;
    tableView.showsHorizontalScrollIndicator = NO;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    
    [self.tableView registerClass:[LXPrepareGetGoodsOrderViewCell class] forCellReuseIdentifier:@"LXOrderNumberViewCell"];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.orderList?self.orderList.count:0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.orderList?[self.orderList[section] product].count:0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OrderListModel * listModel = self.orderList[indexPath.section];
    Product * p = listModel.product[indexPath.row];
    LXPrepareGetGoodsOrderViewCell *orderCell = [tableView dequeueReusableCellWithIdentifier:@"LXOrderNumberViewCell"];
    [orderCell.goodsImage sd_setImageWithURL:[NSURL URLWithString:p.small_pic_wap] placeholderImage:nil];
    orderCell.nameLabel.text = p.name;
    orderCell.priceLabel.text = [NSString stringWithFormat:@"¥%@",p.price];
    orderCell.shopNameLabel.text = @"绿稀野生专卖店";
    orderCell.countLabel.text = [NSString stringWithFormat:@"X%@",p.nums];
    orderCell.introduceLabel.text = @"";
    CGFloat total = [p.price floatValue]*[p.nums integerValue];
    orderCell.payLabel.text = [NSString stringWithFormat:@"¥ %.2f",total];
    orderCell.shop_countLabel.text = [NSString stringWithFormat:@" %@",p.nums];
    
    
    [orderCell.checkLogisticsButton setTitle:@"付款" forState:UIControlStateNormal];
    orderCell.enableUnaderLine = YES;
    orderCell.roundStyle = RoundStyleNone;
 
    
    return orderCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return MineRatio(130);
}


#pragma mark - UITableViewDelegate

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return MineRatio(40);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return MineRatio(80);
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    OrderListModel * listModel = self.orderList[section];
    OderSectionHeaderView *headerView = [[OderSectionHeaderView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, MineRatio(40))];
    headerView.orderNumberLabel.text = listModel.order_id;
    if([listModel.pay_status isEqualToString:@"0"]){
        headerView.orderStatusLabel.text = @"待付款";
    }
    if([listModel.pay_status isEqualToString:@"1"]){
        headerView.orderStatusLabel.text = @"收货";
    }
    [headerView setNeedsLayout];
    return headerView;
}
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    OrderListModel * listModel = self.orderList[section];
    OderSectionFooterView * footerView = [[OderSectionFooterView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, MineRatio(80))];
    footerView.goodsLabel.text = listModel.cost_item;
    [footerView setPrice:[listModel.total_amount floatValue]];
    [footerView.optionButton setTitle:@"付款" forState:UIControlStateNormal];
    footerView.section = section;
    footerView.delegate = self;
    return footerView;
}

-(void)OderSectionFooterView:(OderSectionFooterView *)view didClickOptionButton:(UIButton *)button{
    OrderListModel * list = self.orderList[view.section];
    if([button.currentTitle isEqualToString:@"付款"]){
        NSMutableString * mstr = [NSMutableString string];
        for (Product * p in list.product) {
            [mstr appendString:p.name];
        }
        [self.view alertWithTitle:@"提示" mesege:@"选择实付方式" buttons:@[@"支付宝"] handle:^(NSInteger buttonIndex) {
            [ShopCartManager payForOrderID:list.order_id price:[list.total_amount floatValue] desc:mstr complete:^(BOOL success) {
                if(success){
                    [self.view alertWithTitle:@"提示" mesege:@"支付成功" buttons:@[@"✌️"] handle:^(NSInteger buttonIndex) {
                        
                    }];
                }else{
                    [self.view alertWithTitle:@"提示" mesege:@"支付失败" buttons:@[@"哦"] handle:^(NSInteger buttonIndex) {
                        
                    }];
                }
            }];
        }];
    }
}
@end
